Attribute VB_Name = "sap_40b_data_extract"
'---------------------------------------------------------------------------------------
' Module    : CURRENT_DEV
' Author    : 10096168
' Date      : 2011/10/18
' Purpose   : CURRENT CODE TO EXTRACT / FORMAT DATA FROM 4.0B
'---------------------------------------------------------------------------------------

Option Compare Database
Option Explicit

'---------------------------------------------------------------------------------------
' Procedure : TEXT_FORMAT
' Author    : 10096168
' Date      : 2012/02/02
' Purpose   : WORKING - ADD ERROR HANDLING | CHECK NAMING
'---------------------------------------------------------------------------------------
'
Private Sub TEXT_FORMAT(ByRef FILENAME As String)
Dim objFSO As FileSystemObject
Dim objStreamRead As TextStream
Dim objStreamAppend As TextStream

Dim intTemp As Integer
Dim lngLine As Long
Dim lngLast As Long
Dim strLine As String
Dim strFilename As String
    
    Set objFSO = New FileSystemObject
    Set objStreamRead = objFSO.OpenTextFile(FILENAME, ForReading)
    
    objStreamRead.ReadAll
    lngLast = objStreamRead.Line
    objStreamRead.Close
    
    Set objStreamRead = objFSO.OpenTextFile(FILENAME, ForReading)
    intTemp = 0
    
    Do: DoEvents 'FIND AVALIABLE FILENAME
        strFilename = Replace(FILENAME, ").txt", intTemp & "F).txt")
        intTemp = (intTemp + 1)
    Loop Until OBJECT_EXISTS(strFilename) = False
    
    objFSO.CreateTextFile strFilename
    Set objStreamAppend = objFSO.OpenTextFile(strFilename, ForAppending)
    
    Do Until objStreamRead.AtEndOfStream: DoEvents
        strLine = objStreamRead.ReadLine
        lngLine = objStreamRead.Line
        Select Case lngLine
            Case 2, 3, 5, lngLast 'SKIP THESE LINES
            Case Else
                objStreamAppend.Write strLine & vbNewLine
        End Select
    Loop
    
    objStreamRead.Close
    objStreamAppend.Close
    
    If Not objFSO Is Nothing Then Set objFSO = Nothing
    If Not objStreamRead Is Nothing Then Set objStreamRead = Nothing
    If Not objStreamAppend Is Nothing Then Set objStreamAppend = Nothing
    
    FILENAME = strFilename
End Sub

'---------------------------------------------------------------------------------------
' Procedure : OBJECT_EXISTS
' Author    : 10096168
' Date      : 2012/02/02
' Purpose   : WORKING
'---------------------------------------------------------------------------------------
'
Private Function OBJECT_EXISTS(ByVal FILENAME As String) As Boolean
Dim intTemp As Integer

    On Error Resume Next
    intTemp = GetAttr(FILENAME)
    
    Select Case Err.Number
        Case Is = 0
            OBJECT_EXISTS = True
        Case Else
            OBJECT_EXISTS = False
    End Select
    
    On Error GoTo 0
End Function

'---------------------------------------------------------------------------------------
' Procedure : OBJECT_FILENAME
' Author    : 10096168
' Date      : 2012/02/02
' Purpose   : WORKING - ADD ERROR HANDLING
'---------------------------------------------------------------------------------------
'
Private Function OBJECT_FILENAME(ByVal FILENAME As String, Optional ByVal FILETYPE As String = "txt") As String
Dim intTemp As Integer
Dim strPath As String
    
    strPath = CurrentProject.Path
    intTemp = 0
    Do: DoEvents 'FIND AVALIABLE FILENAME
        OBJECT_FILENAME = Replace(strPath & "\" & FILENAME, "." & FILETYPE, "(" & intTemp & ")." & FILETYPE)
        intTemp = (intTemp + 1)
    Loop Until OBJECT_EXISTS(OBJECT_FILENAME) = False
End Function

'---------------------------------------------------------------------------------------
' Procedure : OBJECT_IMPORT
' Author    : 10096168
' Date      : 2012/02/03
' Purpose   : TEST AND ERROR HANDLING
'---------------------------------------------------------------------------------------
'
Private Sub OBJECT_IMPORT(ByVal FILENAME As String)
Dim strTemp As String
Dim strSPEC As String
Dim strTable As String

    If OBJECT_EXISTS(FILENAME) = True Then
        strTemp = Right(FILENAME, Len(FILENAME) - InStrRev(FILENAME, "\"))
        Select Case Left(strTemp, 4)
            Case "MA61"
                strSPEC = "SAP40B_" & Left(strTemp, 5)
                strTable = "SAP_" & Left(strTemp, 5)
            Case "MARA", "MAKT", "MBEW", "MLAN", "MARM", "MVKE", "MLGT", "MARD", "MLGN"
                strSPEC = "SAP40B_" & Left(strTemp, 4)
                strTable = "SAP_" & Left(strTemp, 4)
            Case "MARC"
                strSPEC = "SAP40B_" & Left(strTemp, 6)
                strTable = "SAP_" & Left(strTemp, 6)
        End Select
        
        DoCmd.TransferText acImportDelim, strSPEC, strTable, FILENAME
    End If
End Sub

'---------------------------------------------------------------------------------------
' Procedure : DB_CHECKBOX
' Author    : 10096168
' Date      : 2012/02/02
' Purpose   : WORKING - ADD ERROR HANDLING
'---------------------------------------------------------------------------------------
'
Private Function DB_CHECKBOX(ByVal TABLE As String, ByVal FIELD As String) As Boolean
Dim SQL As String
Dim rstTemp As Recordset
Const strTable As String = "tbl_SAP_TABLE-FIELDS"
    
    SQL = "SELECT * FROM [" & strTable & "] WHERE [" & strTable & "].[TABLE]='" & TABLE & "' AND [" & strTable & "].[FIELD]='" & FIELD & "';"
    Set rstTemp = CurrentDb.OpenRecordset(SQL)
    
    If rstTemp.RecordCount <> 0 Then DB_CHECKBOX = True 'DEFAULT IS FALSE
    
    If Not rstTemp Is Nothing Then Set rstTemp = Nothing
End Function

'---------------------------------------------------------------------------------------
' Procedure : DB_DELETE_RECORDS
' Author    : 10096168
' Date      : 2012/02/03
' Purpose   : WORKING - ADD ERROR HANDLING
'---------------------------------------------------------------------------------------
'
Private Sub DB_DELETE_RECORDS(ByVal TABLE As String, ByVal WERKS As String, Optional ByVal MASS_DELETE As Boolean = False)
Dim SQL As String
    
    DoCmd.SetWarnings False
    If MASS_DELETE = False Then
        Select Case TABLE
            Case "MA61V"
                SQL = "DELETE * FROM SAP_" & TABLE & " WHERE SAP_" & TABLE & ".WERKS='" & WERKS & "';"
                DoCmd.RunSQL SQL
            Case "MARA", "MEAN", "MBEW", "MLAN", "MARM", "MVKE", "MLGT", "MLGN"
'            SQL = "Delete * " & _
'            "FROM SAP_" & TABLE & " " & _
'            "WHERE (((SAP_" & TABLE & ".MATNR) In (SELECT SAP_MA61V.MATNR FROM SAP_MA61V WHERE (((SAP_MA61V.WERKS)='1211'));)));"
'            Debug.Print SQL
                SQL = "DELETE FROM SAP_" & TABLE & " AS A " & _
                "WHERE EXISTS (SELECT B.MATNR FROM SAP_MA61V AS B WHERE B.WERKS='" & WERKS & "' AND B.MATNR=A.MATNR);"
                DoCmd.RunSQL SQL
            Case "MARD"
                SQL = "DELETE * FROM [SAP_" & TABLE & "] WHERE [SAP_" & TABLE & "].[WERKS]='" & WERKS & "';"
                DoCmd.RunSQL SQL
            Case "MARC"
                SQL = "DELETE * FROM [SAP_" & TABLE & "_A] WHERE [SAP_" & TABLE & "_A].[WERKS]='" & WERKS & "';"
                DoCmd.RunSQL SQL
                SQL = "DELETE * FROM [SAP_" & TABLE & "_B] WHERE [SAP_" & TABLE & "_B].[WERKS]='" & WERKS & "';"
                DoCmd.RunSQL SQL
        End Select
    Else
        Select Case TABLE
            Case "MA61V", "MARA", "MEAN", "MBEW", "MLAN", "MARM", "MVKE", "MLGT", "MLGN", "MARD"
                SQL = "DELETE * FROM SAP_" & TABLE & ";"
                DoCmd.RunSQL SQL
            Case "MARC"
                SQL = "DELETE * FROM SAP_MARC_A;"
                DoCmd.RunSQL SQL
                SQL = "DELETE * FROM SAP_MARC_B;"
                DoCmd.RunSQL SQL
            Case Else
        End Select
    End If
    DoCmd.SetWarnings True
End Sub

'---------------------------------------------------------------------------------------
' Procedure : SAP_CHECKBOX
' Author    : 10096168
' Date      : 2012/02/02
' Purpose   : WORKING - FURTHER TESTING NEEDED
'---------------------------------------------------------------------------------------
'
Private Sub SAP_CHECKBOX(ByVal USER_AREA As Object, ByVal TABLE As String)
Dim SAPObject As Object
Dim strCheckBoxID As String
Dim strLabelText As String
Dim bln21Objects As Boolean
Dim intINSTR As String

Dim intCheckBox As Integer
Dim intLabel As Integer
Dim strLabelID As String

Dim intScroll As Integer
Dim strLabelCur As String
Dim strLabelPrev As String

Dim intError As Integer
    
   On Error GoTo SAP_CHECKBOX_Error
    
    intError = 0
    bln21Objects = False
    intCheckBox = 0
    intLabel = 0
    
    If Not USER_AREA Is Nothing Then
        USER_AREA.Parent.findById("wnd[1]/tbar[0]/btn[14]").press
        For Each SAPObject In USER_AREA.Children: DoEvents
            Select Case SAPObject.Type
                Case "GuiCheckBox"
'                    strCheckBoxID = Replace(SAPObject.id, "/app/con[0]/ses[0]/wnd[1]/usr/", vbNullString)
                    intINSTR = InStr(1, SAPObject.id, "/chk[", vbTextCompare)
                    strCheckBoxID = Right(SAPObject.id, Len(SAPObject.id) - intINSTR)
                    
                    If intCheckBox = 0 Then _
                    intCheckBox = Mid(strCheckBoxID, InStr(1, strCheckBoxID, "[", vbTextCompare) + 1, InStr(1, strCheckBoxID, ",", vbTextCompare) - InStr(1, strCheckBoxID, "[", vbTextCompare) - 1)
                    
                    If StrComp(strCheckBoxID, "chk[" & intCheckBox & ",21]", vbTextCompare) = 0 Then bln21Objects = True
                Case "GuiLabel"
                    strLabelText = SAPObject.Text
'                    strLabelID = Replace(SAPObject.id, "/app/con[0]/ses[0]/wnd[1]/usr/", vbNullString)
                    
                    intINSTR = InStr(1, SAPObject.id, "/lbl[", vbTextCompare)
                    strLabelID = Right(SAPObject.id, Len(SAPObject.id) - intINSTR)
                    
                    If StrComp(strCheckBoxID, vbNullString, vbTextCompare) <> 0 And intLabel = 0 Then _
                    intLabel = Mid(strLabelID, InStr(1, strLabelID, "[", vbTextCompare) + 1, InStr(1, strLabelID, ",", vbTextCompare) - InStr(1, strLabelID, "[", vbTextCompare) - 1)
                    
                    If DB_CHECKBOX(TABLE, strLabelText) = True Then USER_AREA.findById(strCheckBoxID).Selected = True
            End Select
        Next SAPObject
        
        If bln21Objects = True Then 'START SCROLLING CHECK
            intScroll = 0
            
            Do: DoEvents
                strLabelPrev = USER_AREA.findById("lbl[" & intLabel & ",21]").Text
                intScroll = (intScroll + 1)
                USER_AREA.verticalScrollbar.position = intScroll
                intError = 1
                strLabelCur = USER_AREA.findById("lbl[" & intLabel & ",21]").Text
                intError = 0
                If StrComp(strLabelPrev, strLabelCur, vbTextCompare) = 0 Then
                    Exit Do
                Else
                    If DB_CHECKBOX(TABLE, strLabelCur) = True Then USER_AREA.findById("chk[" & intCheckBox & ",21]").Selected = True
                End If
            Loop
        End If
        intError = 2
        USER_AREA.Parent.findById("wnd[1]/tbar[0]/btn[0]").press
        intError = 0
    End If

   On Error GoTo 0
   Exit Sub

SAP_CHECKBOX_Error:
    If intError = 1 Then
        strLabelCur = strLabelPrev
        Resume Next
    ElseIf intError = 2 Then
        USER_AREA.Parent.findById("wnd[1]/tbar[0]/btn[6]").press
        Resume Next
    Else
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure SAP_CHECKBOX of Module CURRENT_DEV"
    End If
End Sub

'---------------------------------------------------------------------------------------
' Procedure : SAP_MA61V
' Author    : 10096168
' Date      : 2012/02/02
' Purpose   : WORKING - ADD ERROR HANDLING
'---------------------------------------------------------------------------------------
'
Private Sub SAP_MA61V(ByVal WERKS As String, ByRef FILENAME As String, Optional ByVal LVORM_X As Boolean = False, _
Optional ByVal MMSTA_36 As Boolean = False, Optional ByVal MSTAE_36 As Boolean = False)
Dim SAPApp As Object
Dim SAPGUI As Object
Dim SAPConnect As Object
Dim SAPSession As Object
Dim WScript As Object
Dim SAPUserArea As Object
    
    If SAPApp Is Nothing Then
        Set SAPGUI = GetObject("SAPGUI")
        Set SAPApp = SAPGUI.GetScriptingEngine
    End If
    If SAPConnect Is Nothing Then
       Set SAPConnect = SAPApp.Children(0)
    End If
    If SAPSession Is Nothing Then
       Set SAPSession = SAPConnect.Children(0)
    End If
    If Not WScript Is Nothing Then
       WScript.ConnectObject SAPSession, "on"
       WScript.ConnectObject SAPApp, "on"
    End If
    
    With SAPSession
        .findById("wnd[0]/tbar[0]/okcd").Text = "ZTABDIS" 'TCODE
        .findById("wnd[0]").sendVKey 0
        .findById("wnd[0]/usr/ctxtP_TABNAM").Text = "MA61V" 'TABLE
        .findById("wnd[0]").sendVKey 8
        
        .findById("wnd[0]/mbar/menu[3]/menu[1]").Select
        .findById("wnd[1]/usr/radSEUCUSTOM-FIELDNAME").Select 'TECH NAMES
        .findById("wnd[1]/usr/ctxtRSEUMOD-TBLISTBR").Text = "250" 'WIDTH OF OUTPUT
        .findById("wnd[1]/usr/txtRSEUMOD-TBMAXSEL").Text = "99,999,999" 'MAXIMUM # OF HITS
        .findById("wnd[1]/tbar[0]/btn[0]").press
        
        .findById("wnd[0]/mbar/menu[3]/menu[2]").Select
        Set SAPUserArea = .findById("wnd[1]/usr/")
        Call SAP_CHECKBOX(SAPUserArea, "MA61V")
        
        .findById("wnd[0]/usr/ctxtI2-LOW").Text = Trim(WERKS)
        If LVORM_X = True Then
            .findById("wnd[0]/usr/ctxtI4-LOW").Text = "X"
            .findById("wnd[0]/usr/ctxtI4-LOW").SetFocus
            .findById("wnd[0]/usr/ctxtI4-LOW").caretPosition = 1
            .findById("wnd[0]/tbar[1]/btn[2]").press
            .findById("wnd[1]/usr/lbl[2,10]").SetFocus
            .findById("wnd[1]/usr/lbl[2,10]").caretPosition = 0
            .findById("wnd[1]").sendVKey 2
            .findById("wnd[1]/tbar[0]/btn[0]").press
        End If
        If MMSTA_36 = True Then
            .findById("wnd[0]/usr/ctxtI5-LOW").Text = "36"
            .findById("wnd[0]/usr/ctxtI5-LOW").SetFocus
            .findById("wnd[0]/usr/ctxtI5-LOW").caretPosition = 2
            .findById("wnd[0]/tbar[1]/btn[2]").press
            .findById("wnd[1]/usr/lbl[2,10]").SetFocus
            .findById("wnd[1]/usr/lbl[2,10]").caretPosition = 0
            .findById("wnd[1]").sendVKey 2
            .findById("wnd[1]/tbar[0]/btn[0]").press
        End If
        If MSTAE_36 = True Then
            .findById("wnd[0]/usr/ctxtI6-LOW").Text = "36"
            .findById("wnd[0]/usr/ctxtI6-LOW").SetFocus
            .findById("wnd[0]/usr/ctxtI6-LOW").caretPosition = 2
            .findById("wnd[0]/tbar[1]/btn[2]").press
            .findById("wnd[1]/usr/lbl[2,10]").SetFocus
            .findById("wnd[1]/usr/lbl[2,10]").caretPosition = 0
            .findById("wnd[1]").sendVKey 2
        .findById("wnd[1]/tbar[0]/btn[0]").press
        End If
        
        .findById("wnd[0]/tbar[1]/btn[8]").press 'RUNS REPORT
        .findById("wnd[0]/mbar/menu[3]/menu[0]/menu[1]").Select
        Call SAP_CHECKBOX(SAPUserArea, "MA61V")
        
        .findById("wnd[0]/usr/txt[57,0]").Text = "1023" 'SETS REPORT WIDTH
        .findById("wnd[0]").sendVKey 0
        .findById("wnd[0]/mbar/menu[6]/menu[5]/menu[2]/menu[2]").Select
        .findById("wnd[1]/tbar[0]/btn[0]").press
        FILENAME = OBJECT_FILENAME("MA61V_" & WERKS & ".txt") 'RETURN FILE NAME
        .findById("wnd[1]/usr/ctxtRLGRAP-FILENAME").Text = FILENAME  'CHECK IF EXISTS
        .findById("wnd[1]").sendVKey 0
        .findById("wnd[0]/tbar[0]/btn[3]").press
        .findById("wnd[0]/tbar[0]/btn[3]").press
        .findById("wnd[0]/tbar[0]/btn[3]").press
    End With
End Sub

Public Sub SAP_MATNR(ByVal TABLE As String)
Dim SAPApp As Object
Dim SAPGUI As Object
Dim SAPConnect As Object
Dim SAPSession As Object
Dim WScript As Object
Dim SAPUserArea As Object

Dim SQL As String
Dim rstTemp As Recordset
Dim intMATNR As Integer

Dim strFilename As String

    If SAPApp Is Nothing Then
        Set SAPGUI = GetObject("SAPGUI")
        Set SAPApp = SAPGUI.GetScriptingEngine
    End If
    If SAPConnect Is Nothing Then
       Set SAPConnect = SAPApp.Children(0)
    End If
    If SAPSession Is Nothing Then
       Set SAPSession = SAPConnect.Children(0)
    End If
    If Not WScript Is Nothing Then
       WScript.ConnectObject SAPSession, "on"
       WScript.ConnectObject SAPApp, "on"
    End If
    
    With SAPSession
        .findById("wnd[0]/mbar/menu[3]/menu[1]").Select
        .findById("wnd[1]/usr/radSEUCUSTOM-FIELDNAME").Select 'TECH NAMES
        .findById("wnd[1]/usr/ctxtRSEUMOD-TBLISTBR").Text = "250" 'WIDTH OF OUTPUT
        .findById("wnd[1]/usr/txtRSEUMOD-TBMAXSEL").Text = "99,999,999" 'MAXIMUM # OF HITS
        .findById("wnd[1]/tbar[0]/btn[0]").press
        
        .findById("wnd[0]/mbar/menu[3]/menu[2]").Select
        .findById("wnd[1]/tbar[0]/btn[14]").press
        .findById("wnd[1]/usr/chk[2,5]").Selected = True 'MATNR
        .findById("wnd[1]/tbar[0]/btn[0]").press
        .findById("wnd[0]/usr/btn%_I1_%_APP_%-VALU_PUSH").press 'MATNR MULTIPLE ENTRY
        .findById("wnd[1]/tbar[0]/btn[16]").press
        
        intMATNR = 0
'        SQL = "SELECT SAP_MA61V.MATNR " & _
'        "FROM SAP_MA61V " & _
'        "GROUP BY SAP_MA61V.MATNR;"
        SQL = "SELECT SAP_MA61V.MATNR " & _
        "FROM SAP_MA61V " & _
        "WHERE ((Not (SAP_MA61V.MMSTA)='36') AND (Not (SAP_MA61V.MSTAE)='36')) " & _
        "GROUP BY SAP_MA61V.MATNR;"
        Set rstTemp = CurrentDb.OpenRecordset(SQL)
        Do Until rstTemp.EOF: DoEvents
            .findById("wnd[1]/usr/sub:SAPLALDB:2020[0]/ctxtRSCSEL-SLOW_I[0,5]").Text = rstTemp.Fields(0).Value
            .findById("wnd[1]").sendVKey 13
            
            intMATNR = (intMATNR + 1)
            If intMATNR = 1499 Then
                intMATNR = 0
                .findById("wnd[1]/tbar[0]/btn[5]").press
                .findById("wnd[0]/tbar[1]/btn[8]").press 'RUNS REPORT
                .findById("wnd[0]/mbar/menu[3]/menu[0]/menu[1]").Select
                If SAPUserArea Is Nothing Then Set SAPUserArea = .findById("wnd[1]/usr/")
                Call SAP_CHECKBOX(SAPUserArea, TABLE)
                
                .findById("wnd[0]/usr/txt[57,0]").Text = "1023" 'SETS REPORT WIDTH
                .findById("wnd[0]").sendVKey 0
                
                .findById("wnd[0]/mbar/menu[6]/menu[5]/menu[2]/menu[2]").Select
                .findById("wnd[1]/tbar[0]/btn[0]").press
                strFilename = OBJECT_FILENAME(TABLE & ".txt")   'CHECK IF EXISTS
                .findById("wnd[1]/usr/ctxtRLGRAP-FILENAME").Text = strFilename
                .findById("wnd[1]").sendVKey 0
                .findById("wnd[0]/tbar[0]/btn[3]").press
                .findById("wnd[0]/tbar[0]/btn[3]").press
                .findById("wnd[0]/tbar[0]/btn[3]").press 'START NEW SESSION
                
                'ADD IMPORT FUNCTION
                Call TEXT_FORMAT(strFilename)
                Call OBJECT_IMPORT(strFilename)

                .findById("wnd[0]/tbar[0]/okcd").Text = "ZTABDIS" 'TCODE
                .findById("wnd[0]").sendVKey 0
                .findById("wnd[0]/usr/ctxtP_TABNAM").Text = TABLE
                .findById("wnd[0]").sendVKey 8 'DISPLAYS TABLE
                .findById("wnd[0]/mbar/menu[3]/menu[2]").Select
                .findById("wnd[1]/tbar[0]/btn[14]").press
                .findById("wnd[1]/usr/chk[2,5]").Selected = True 'MATNR
                .findById("wnd[1]/tbar[0]/btn[0]").press
                .findById("wnd[0]/usr/btn%_I1_%_APP_%-VALU_PUSH").press 'MATNR MULTIPLE ENTRY
                .findById("wnd[1]/tbar[0]/btn[16]").press
            End If
            rstTemp.MoveNext
        Loop
        
        If 0 > intMATNR <= 1499 Then
            .findById("wnd[1]/tbar[0]/btn[5]").press
            .findById("wnd[0]/tbar[1]/btn[8]").press 'RUNS REPORT
            .findById("wnd[0]/mbar/menu[3]/menu[0]/menu[1]").Select
            If SAPUserArea Is Nothing Then Set SAPUserArea = .findById("wnd[1]/usr/")
            Call SAP_CHECKBOX(SAPUserArea, TABLE)
            
            .findById("wnd[0]/usr/txt[57,0]").Text = "1023" 'SETS REPORT WIDTH
            .findById("wnd[0]").sendVKey 0
            
            .findById("wnd[0]/mbar/menu[6]/menu[5]/menu[2]/menu[2]").Select
            .findById("wnd[1]/tbar[0]/btn[0]").press
            strFilename = OBJECT_FILENAME(TABLE & ".txt")   'CHECK IF EXISTS
            .findById("wnd[1]/usr/ctxtRLGRAP-FILENAME").Text = strFilename
            .findById("wnd[1]").sendVKey 0
            .findById("wnd[0]/tbar[0]/btn[3]").press
            .findById("wnd[0]/tbar[0]/btn[3]").press
            .findById("wnd[0]/tbar[0]/btn[3]").press
            
            'ADD IMPORT FUNCTION
            Call TEXT_FORMAT(strFilename)
            Call OBJECT_IMPORT(strFilename)
        End If
    End With
End Sub

'---------------------------------------------------------------------------------------
' Procedure : SAP_ZTABDIS
' Author    : 10096168
' Date      : 2012/02/03
' Purpose   : WORKING - ADD ERROR HANDLING
'---------------------------------------------------------------------------------------
'
Private Sub SAP_ZTABDIS(ByVal TABLE As String, Optional ByVal WERKS As String = vbNullString)
Dim SAPApp As Object
Dim SAPGUI As Object
Dim SAPConnect As Object
Dim SAPSession As Object
Dim WScript As Object
Dim SAPUserArea As Object

Dim strTemp As String
Dim strFilename As String
    
    If SAPApp Is Nothing Then
        Set SAPGUI = GetObject("SAPGUI")
        Set SAPApp = SAPGUI.GetScriptingEngine
    End If
    If SAPConnect Is Nothing Then
       Set SAPConnect = SAPApp.Children(0)
    End If
    If SAPSession Is Nothing Then
       Set SAPSession = SAPConnect.Children(0)
    End If
    If Not WScript Is Nothing Then
       WScript.ConnectObject SAPSession, "on"
       WScript.ConnectObject SAPApp, "on"
    End If
    
    With SAPSession
        .findById("wnd[0]/tbar[0]/okcd").Text = "ZTABDIS" 'TCODE
        .findById("wnd[0]").sendVKey 0
        
        Select Case TABLE
            Case "MARA", "MARD", "MLGN", "MLGT", "MVKE", "MARM", "MLAN", "MBEW", "MAKT"
                strTemp = TABLE
                .findById("wnd[0]/usr/ctxtP_TABNAM").Text = strTemp 'TABLE
            Case "MARC" 'MARC TABLE NEEDS TO BE SPLIT
                strTemp = "MARC_A"
                .findById("wnd[0]/usr/ctxtP_TABNAM").Text = TABLE 'TABLE
            Case "MARC_B"
                strTemp = TABLE
                .findById("wnd[0]/usr/ctxtP_TABNAM").Text = "MARC" 'TABLE
'            Case "MARD"
'                strTemp = TABLE
'                .findById("wnd[0]/usr/ctxtP_TABNAM").Text = strTemp 'TABLE
        End Select
        
            .findById("wnd[0]").sendVKey 8 'DISPLAYS TABLE
            
        If StrComp(WERKS, vbNullString, vbTextCompare) <> 0 Then
            .findById("wnd[0]/mbar/menu[3]/menu[1]").Select
            .findById("wnd[1]/usr/radSEUCUSTOM-FIELDNAME").Select 'TECH NAMES
            .findById("wnd[1]/usr/ctxtRSEUMOD-TBLISTBR").Text = "250" 'WIDTH OF OUTPUT
            .findById("wnd[1]/usr/txtRSEUMOD-TBMAXSEL").Text = "99,999,999" 'MAXIMUM # OF HITS
            .findById("wnd[1]/tbar[0]/btn[0]").press
        
            .findById("wnd[0]/mbar/menu[3]/menu[2]").Select
            .findById("wnd[1]/tbar[0]/btn[14]").press
            .findById("wnd[1]/usr/chk[2,6]").Selected = True
            .findById("wnd[1]/tbar[0]/btn[0]").press
            .findById("wnd[0]/usr/ctxtI1-LOW").Text = Trim(WERKS)
            .findById("wnd[0]/tbar[1]/btn[8]").press 'RUNS REPORT
            .findById("wnd[0]/mbar/menu[3]/menu[0]/menu[1]").Select
            Set SAPUserArea = .findById("wnd[1]/usr/")
            Call SAP_CHECKBOX(SAPUserArea, strTemp)
            
            .findById("wnd[0]/usr/txt[57,0]").Text = "1023" 'SETS REPORT WIDTH
            .findById("wnd[0]").sendVKey 0
            
            .findById("wnd[0]/mbar/menu[6]/menu[5]/menu[2]/menu[2]").Select
            .findById("wnd[1]/tbar[0]/btn[0]").press
            strFilename = OBJECT_FILENAME(strTemp & "_" & WERKS & ".txt")  'CHECK IF EXISTS
            .findById("wnd[1]/usr/ctxtRLGRAP-FILENAME").Text = strFilename
            .findById("wnd[1]").sendVKey 0
            .findById("wnd[0]/tbar[0]/btn[3]").press
            .findById("wnd[0]/tbar[0]/btn[3]").press
            .findById("wnd[0]/tbar[0]/btn[3]").press
            
            'ADD IMPORT FUNCTION
            Call TEXT_FORMAT(strFilename)
            Call OBJECT_IMPORT(strFilename)
        Else
            Call SAP_MATNR(strTemp)
        End If
        
        If StrComp(TABLE, "MARC", vbTextCompare) = 0 Then Call SAP_ZTABDIS("MARC_B", WERKS)
    End With
End Sub

Private Sub TEST()
Dim SAPApp As Object
Dim SAPGUI As Object
Dim SAPConnect As Object
Dim SAPSession As Object
Dim WScript As Object

Dim strTemp As String
Dim strFilename As String
Dim intPlant As Integer
Dim arrPlant As Variant

Dim intTable As Integer
Dim arrTables(9) As String
    
    If SAPApp Is Nothing Then
        Set SAPGUI = GetObject("SAPGUI")
        Set SAPApp = SAPGUI.GetScriptingEngine
    End If
    If SAPConnect Is Nothing Then
       Set SAPConnect = SAPApp.Children(0)
    End If
    If SAPSession Is Nothing Then
       Set SAPSession = SAPConnect.Children(0)
    End If
    If Not WScript Is Nothing Then
       WScript.ConnectObject SAPSession, "on"
       WScript.ConnectObject SAPApp, "on"
    End If
    
    strTemp = InputBox("Enter desired plants (eg 1000, 1003, 1011, 1037, 1211, 4012)")

    If StrComp(strTemp, vbNullString, vbTextCompare) = 0 Then Exit Sub

    arrPlant = Split(strTemp, ",")

    For intPlant = LBound(arrPlant) To UBound(arrPlant): DoEvents
        If IsNumeric(arrPlant(intPlant)) = True Then
            Call DB_DELETE_RECORDS("MA61V", arrPlant(intPlant)) 'DELETE EXISTING RECORDS
            Call SAP_MA61V(arrPlant(intPlant), strFilename, True, True, True) 'DOWNLOADS MA61V DATA
            Call TEXT_FORMAT(strFilename)
            Call OBJECT_IMPORT(strFilename)
        End If
    Next intPlant
    
    arrTables(0) = "MARA"
    arrTables(1) = "MARC"
    arrTables(2) = "MARD"
    arrTables(3) = "MLGN"
    arrTables(4) = "MLGT"
    arrTables(5) = "MVKE"
    arrTables(6) = "MARM"
    arrTables(7) = "MLAN"
    arrTables(8) = "MBEW"
    arrTables(9) = "MAKT"
    
    For intTable = LBound(arrTables) To UBound(arrTables): DoEvents
        For intPlant = LBound(arrPlant) To UBound(arrPlant): DoEvents 'CLEARS TABLES FOR SPECIFIC PLANT
            If IsNumeric(arrPlant(intPlant)) = True Then Call DB_DELETE_RECORDS(arrTables(intTable), arrPlant(intPlant), True)   'DELETE EXISTING RECORDS
        Next intPlant

        If StrComp(arrTables(intTable), "MARC", vbTextCompare) = 0 Or StrComp(arrTables(intTable), "MARD", vbTextCompare) = 0 Then
        Else
            If StrComp(arrTables(intTable), vbNullString, vbTextCompare) <> 0 Then SAP_ZTABDIS (arrTables(intTable)) 'DOWNLOADS TABLE DATA
        End If
    Next intTable
    
    For intPlant = LBound(arrPlant) To UBound(arrPlant): DoEvents
        If IsNumeric(arrPlant(intPlant)) = True Then
            Call SAP_ZTABDIS("MARC", arrPlant(intPlant))
            Call SAP_ZTABDIS("MARD", arrPlant(intPlant))
        End If
    Next intPlant
    
'    For intTemp = LBound(arrTables) To UBound(arrTables)
'        Call DB_DELETE_RECORDS(arrTables(intTemp), "1211") 'DELETE EXISTING RECORDS
'        If StrComp(arrTables(intTemp), "MARC", vbTextCompare) = 0 Or StrComp(arrTables(intTemp), "MARD", vbTextCompare) = 0 Then
'            Call SAP_ZTABDIS(arrTables(intTemp), "1211") 'DOWNLOADS MA61V DATA
'        Else
'            Call SAP_ZTABDIS(arrTables(intTemp))  'DOWNLOADS MA61V DATA
'        End If
'        Call TEXT_FORMAT(strFilename)
'        Call OBJECT_IMPORT(strFilename)
'    Next intTemp
    
'    Call SAP_ZTABDIS("MARA")
'    Call SAP_ZTABDIS("MARC", WERKS)
'    Call SAP_ZTABDIS("MARD", "1211")
'    Call SAP_ZTABDIS("MLGN")
'    Call SAP_ZTABDIS("MLGT")
'    Call SAP_ZTABDIS("MVKE")
'    Call SAP_ZTABDIS("MARM")
'    Call SAP_ZTABDIS("MLAN")
'    Call SAP_ZTABDIS("MBEW")
'    Call SAP_ZTABDIS("MEAN")
End Sub

Private Sub TEST_LOAD_MARC()
Dim arrFile(23) As String
Dim intTemp As Integer

    arrFile(0) = "C:\Users\10096168\Desktop\OldDesktop\sandbox\everest\w2_dl1\database_dev\marc\MARC_A_1000(00F).txt"
    arrFile(1) = "C:\Users\10096168\Desktop\OldDesktop\sandbox\everest\w2_dl1\database_dev\marc\MARC_A_1000(10F).txt"
    arrFile(2) = "C:\Users\10096168\Desktop\OldDesktop\sandbox\everest\w2_dl1\database_dev\marc\MARC_A_1003(00F).txt"
    arrFile(3) = "C:\Users\10096168\Desktop\OldDesktop\sandbox\everest\w2_dl1\database_dev\marc\MARC_A_1003(10F).txt"
    arrFile(4) = "C:\Users\10096168\Desktop\OldDesktop\sandbox\everest\w2_dl1\database_dev\marc\MARC_A_1011(00F).txt"
    arrFile(5) = "C:\Users\10096168\Desktop\OldDesktop\sandbox\everest\w2_dl1\database_dev\marc\MARC_A_1011(10F).txt"
    arrFile(6) = "C:\Users\10096168\Desktop\OldDesktop\sandbox\everest\w2_dl1\database_dev\marc\MARC_A_1037(00F).txt"
    arrFile(7) = "C:\Users\10096168\Desktop\OldDesktop\sandbox\everest\w2_dl1\database_dev\marc\MARC_A_1037(10F).txt"
    arrFile(8) = "C:\Users\10096168\Desktop\OldDesktop\sandbox\everest\w2_dl1\database_dev\marc\MARC_A_1211(00F).txt"
    arrFile(9) = "C:\Users\10096168\Desktop\OldDesktop\sandbox\everest\w2_dl1\database_dev\marc\MARC_A_1211(10F).txt"
    arrFile(10) = "C:\Users\10096168\Desktop\OldDesktop\sandbox\everest\w2_dl1\database_dev\marc\MARC_A_4012(00F).txt"
    arrFile(11) = "C:\Users\10096168\Desktop\OldDesktop\sandbox\everest\w2_dl1\database_dev\marc\MARC_A_4012(10F).txt"
    arrFile(12) = "C:\Users\10096168\Desktop\OldDesktop\sandbox\everest\w2_dl1\database_dev\marc\MARC_B_1000(00F).txt"
    arrFile(13) = "C:\Users\10096168\Desktop\OldDesktop\sandbox\everest\w2_dl1\database_dev\marc\MARC_B_1000(10F).txt"
    arrFile(14) = "C:\Users\10096168\Desktop\OldDesktop\sandbox\everest\w2_dl1\database_dev\marc\MARC_B_1003(00F).txt"
    arrFile(15) = "C:\Users\10096168\Desktop\OldDesktop\sandbox\everest\w2_dl1\database_dev\marc\MARC_B_1003(10F).txt"
    arrFile(16) = "C:\Users\10096168\Desktop\OldDesktop\sandbox\everest\w2_dl1\database_dev\marc\MARC_B_1011(00F).txt"
    arrFile(17) = "C:\Users\10096168\Desktop\OldDesktop\sandbox\everest\w2_dl1\database_dev\marc\MARC_B_1011(10F).txt"
    arrFile(18) = "C:\Users\10096168\Desktop\OldDesktop\sandbox\everest\w2_dl1\database_dev\marc\MARC_B_1037(00F).txt"
    arrFile(19) = "C:\Users\10096168\Desktop\OldDesktop\sandbox\everest\w2_dl1\database_dev\marc\MARC_B_1037(10F).txt"
    arrFile(20) = "C:\Users\10096168\Desktop\OldDesktop\sandbox\everest\w2_dl1\database_dev\marc\MARC_B_1211(00F).txt"
    arrFile(21) = "C:\Users\10096168\Desktop\OldDesktop\sandbox\everest\w2_dl1\database_dev\marc\MARC_B_1211(10F).txt"
    arrFile(22) = "C:\Users\10096168\Desktop\OldDesktop\sandbox\everest\w2_dl1\database_dev\marc\MARC_B_4012(00F).txt"
    arrFile(23) = "C:\Users\10096168\Desktop\OldDesktop\sandbox\everest\w2_dl1\database_dev\marc\MARC_B_4012(10F).txt"
    
    For intTemp = LBound(arrFile) To UBound(arrFile)
        Call OBJECT_IMPORT(arrFile(intTemp))
    Next intTemp
End Sub

